#include "WebPage.h"
#include "simhash/Simhasher.hpp"
#include <iostream>

using namespace simhash;
using namespace tinyxml2;

Simhasher WebPage::simhasher("/home/kyle/Project/WebDisk/include/cppjieba/dict/jieba.dict.utf8",
                             "/home/kyle/Project/WebDisk/include/cppjieba/dict/hmm_model.utf8",
                             "/home/kyle/Project/WebDisk/include/cppjieba/dict/idf.utf8",
                             "/home/kyle/Project/WebDisk/include/cppjieba/dict/stop_words.utf8");

WebPage::WebPage(string &doc)
    : _doc(doc) {
    // 计算simhash
    size_t topN = 5;
    uint64_t u64 = 0;
    WebPage::simhasher.make(_doc, topN, u64);
    _dochash = u64;
}

// int WebPage::getDocid() {
//     return _docid;
// }

string WebPage::getDoc() {
    return _doc;
}

// string WebPage::getContent() {
//     return _docContent;
// }
uint64_t WebPage::getHash() {
    return _dochash;
}

map<string, int> &WebPage::getWordsMap() {
    // undo
    return _wordsMap;
}

bool operator==(const WebPage &lhs, const WebPage &rhs) {
    return Simhasher::isEqual(lhs._dochash, rhs._dochash);
}

bool operator<(const WebPage &lhs, const WebPage &rhs) {
    // undo
    return lhs._docid < rhs._docid;
}

ostream &operator<<(ostream &os, const WebPage &page) {
    // os << "<doc>" << "\n";
    // os << "\t<docid>" << page._docid << "</docid>" << "\n";
    // os << "\t<title>" << page._docTitle << "</title>" << "\n";
    // os << "\t<link>" << page._docUrl << "</link>" << "\n";
    // os << "\t<content>" << page._docContent << "</content>" << "\n";
    // os << "</doc>";
    os << page._doc;
    return os;
}

void WebPage::setDocid(int docid) {
    _docid = docid;
}